// index.ts
// 获取应用实例
import { requestPUT, requestGET } from '../../utils/service'
Component({
  data: {
    loading: false,
    agree: false,
    showYSModal: false,
    showYSModalType: '',
    endDate: new Date(new Date().getFullYear() - 1, 11, 31).toLocaleDateString(),
    sexArray: [
      '男', '女'
    ],
    registerInfo: {
      name: '',
      sex: '',
      phone: '',
      birthData: '2023/12/31',
    },
  },
  created() {
    console.log('endDate===',this.data.endDate);

  },
  methods: {
    bindchange(e: any) {
      console.log(e)
      this.setData({
        agree: !this.data.agree
      })
    },
    showModal(e: any) {
      console.log(e.target.dataset.type)
      this.setData({
        showYSModalType: e.target.dataset.type,
        showYSModal: true
      })
    },
    closeModal() {
      this.setData({
        showYSModal: false
      })
    },
    // 输入姓名
    inputName(v: any) {
      let registerInfo = this.data.registerInfo
      registerInfo.name = v.detail.value
      this.setData({
        registerInfo: registerInfo
      })
    },
    // 性别选择
    pickerSex(v: any) {
      let registerInfo = this.data.registerInfo
      registerInfo.sex = v.detail.value
      this.setData({
        registerInfo: registerInfo
      })
    },
    // 获取手机号
    getPhoneNumber(e: any) {
      console.log(e.detail.code)  // 动态令牌
      console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
      console.log(e.detail.errno)  // 错误码（失败时返回）
      if (e.detail.code) {
        requestGET("user/getPhoneByCode", { code: e.detail.code })
          .then((res: any) => {
            let registerInfo = this.data.registerInfo
            registerInfo.phone = res.data.data
            this.setData({
              registerInfo: registerInfo
            })
          })
      }
    },
    // 生日选择
    pickerBirth(v: any) {
      console.log(v)
      let registerInfo = this.data.registerInfo
      registerInfo.birthData = v.detail.value
      this.setData({
        registerInfo: registerInfo
      })
    },
    // 提交注册
    submit() {
      // 非空校验
      let errMsg = ''
      if (!this.data.registerInfo.name) {
        errMsg = '请填写姓名'
      } else if (!this.data.registerInfo.sex) {
        errMsg = '请选择性别'
      } else if (!this.data.registerInfo.phone) {
        errMsg = '请填写手机号'
      } else if (!this.data.registerInfo.birthData || this.data.registerInfo.birthData=='2023/12/31') {
        errMsg = '请选择出生日期'
      }
      if (errMsg) {
        wx.showToast({
          title: errMsg,
          icon: 'error',
          duration: 2000
        })
        return
      }
      if (!this.data.agree) {
        wx.showToast({
          title: "需勾选同意协议",
          icon: 'error',
          duration: 2000
        })
        return
      }
      const data = {
        "userName": this.data.registerInfo.name,
        "gender": this.data.sexArray[Number(this.data.registerInfo.sex)],
        "birthDay": this.data.registerInfo.birthData,
        "phone": this.data.registerInfo.phone,
        "openId": wx.getStorageSync('openId'),
        "wxAuthenticationCode": wx.getStorageSync('code'),
      }
      this.setData({ loading: true })
      requestPUT("user/register", data)
        .then((res: any) => {
          console.log(res);
          let that = this
          if (res.data.code == 200) {
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  that.setData({ loading: false })
                  // 跳转到首页
                  wx.navigateTo({
                    url: '/pages/home/index',
                  })
                }, 1000)
              }
            })

          } else {
            that.setData({ loading: false })
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            });
          }
        })
    },
  },
})
